const {
    getById,        // 根据 id 查询权限     
    getList,        // 获取权限列表     
    create,         // 添加权限     
    edit,           // 编辑权限 
    remove,         // 删除权限 
} = require("../../service/user/rule")


exports.ruleApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/")[1]
    const { id } = ctx.params || "";
    const { rulename, summary, creator, pageNum, pageSize, updator } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (ctx.request.url.length > url.length + 1) {
            res = await getById({ id })
        } else {
            res = await getList({ pageNum, pageSize })
        }; break;
        case "POST": res = await create({ rulename, summary, creator }); break;
        case "PUT": res = await edit({ summary, updator, id }); break;
        case "DELETE": res = await remove({ id }); break;
        default: res = {msg:"操作失败", code:"400"}
    }
    ctx.body = res
}